<template>
  <div class="divBox relative">
    <el-card class="box-card" style="min-height: 80vh">
      <div slot="header" class="clearfix" style="margin-bottom: -20px ;">
        <div class="container">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
            <el-form-item label="模块：" >
              <el-radio-group  v-model="queryForm.groupmodule" size="small" @change="search">
                <el-radio-button label="">所有模块</el-radio-button>
                <el-radio-button v-for="item in groupModuleDataList" :label="item.id" >
                  {{item.name}}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="类型:" >
              <el-radio-group  v-model="queryForm.grouptype" size="small" @change="search">
                <el-radio-button label="">全部类型</el-radio-button>
                <el-radio-button v-for="item in groupTypeDataList" :label="item.id" >
                  {{item.name}}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
            <br/>
            <el-form-item>
              <el-input v-model="queryForm.groupname" placeholder="请输入分组名称" clearable @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="search()">查询</el-button>
              <el-button v-if="checkPermi(['softwaregroup:index:save'])" type="primary" @click="addOrUpdateHandle()">新增数据</el-button>
              <el-button v-if="checkPermi(['softwaregroup:index:delete'])"  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;" size="mini" :highlight-current-row="true" :header-cell-style=" {fontWeight:'bold'}">
        <el-table-column type="selection" header-align="center" align="center" width="50"/>
           <el-table-column prop="id" label="id" header-align="center" align="center" :show-overflow-tooltip="true" min-width="30"></el-table-column>
           <el-table-column prop="groupname" label="名称" header-align="center"  :show-overflow-tooltip="true"></el-table-column>
<!--           <el-table-column prop="icon" label="图标" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>-->
           <el-table-column prop="groupimg" label="展示图" header-align="center" align="center" :show-overflow-tooltip="true">
             <template slot-scope="scope"  >
               <el-image  fit="scaleDown" :src="scope.row.groupimg" :preview-src-list="[scope.row.groupimg]">
               </el-image>
             </template>
           </el-table-column>
<!--           <el-table-column prop="groupvideo" label="视频" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>-->
           <el-table-column prop="grouptype" label="类型" >
             <template slot-scope="scope"  >
               <el-tag type="success" v-if="scope.row.grouptype==1" >单列无背景图</el-tag>
               <el-tag type="success" v-else-if="scope.row.grouptype==2" >单列有背景图</el-tag>
               <el-tag type="success" v-else-if="scope.row.grouptype==3" >双列无背景图</el-tag>
               <el-tag type="danger" v-else width="160px">双列有背景图</el-tag>
             </template>
           </el-table-column>
           <el-table-column prop="groupmodule" label="模块" >
             <template slot-scope="scope"  >
               <el-tag type="danger" v-if="scope.row.groupmodule==1" >首页</el-tag>
               <el-tag type="danger" v-else-if="scope.row.groupmodule==2" >云玩</el-tag>
             </template>
           </el-table-column>
           <el-table-column prop="status" label="状态">
             <template slot-scope="scope" >
               <el-switch
                 v-model="scope.row.status==1"
                 :active-value="true"
                 :inactive-value="false"
                 active-text="上架"
                 inactive-text="下架"
                 @change="change(scope.row)"
               />
             </template>
           </el-table-column>
           <el-table-column prop="sortnum" label="排序" header-align="center" align="center" :show-overflow-tooltip="true"></el-table-column>
           <el-table-column prop="createtime" label="创建日期" header-align="center" align="center" width="150" :show-overflow-tooltip="true"></el-table-column>
           <el-table-column prop="updatetime" label="更新日期" header-align="center" align="center" width="150" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text"  v-if="checkPermi(['softwaregroup:index:update'])" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text"  v-if="checkPermi(['softwaregroup:index:delete'])" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
              @size-change="sizeChangeHandle"
              @current-change="currentChangeHandle"
              :current-page="tableForm.pageIndex"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="tableForm.pageSize"
              :total="tableForm.total"
              layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-card>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './softwaregroup-add-and-update'
  import api from '@/api/api'
  import {checkPermi} from "@/utils/permission";

  export default {
    components: {
      AddOrUpdate
    },
    data () {
      return {
        dataList: [],
        groupModuleDataList:[],
        groupTypeDataList:[],
        queryForm: {
          groupmodule:'',
          groupname: '',
          grouptype:""
        },
        tableForm:{
          pageIndex: 1,
          pageSize: 10,
          total: 0,
        },
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    mounted () {
      this.getConfig()
    },
    methods: {
      checkPermi,
      search(){
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.dataListLoading = true
        let postData={
          'order':'desc',
          'column':'sortnum,createtime'
        }
        Object.assign(postData,this.queryForm, this.tableForm)
        postData.groupname=api.getLikeValue(postData.groupname)
        //postData.key=postData.key.length==0?postData.key:'%'+postData.key+'%'
          api.post('rest/vabc/softwaregroup/list',postData).then(res => {
            this.dataList=res.datalist
            this.tableForm.total=res.total
          }).finally(() => {
            this.dataListLoading = false
          });
      },

      // 每页数
      sizeChangeHandle (val) {
        this.tableForm.pageSize = val
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.tableForm.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,this.groupModuleDataList,this.groupTypeDataList)
        })
      },
      change(row){
        let postData={id:row.id}
        postData.status=row.status==1?0:1;
        api.post('rest/vabc/softwaregroup/updateById',postData).then(res => {
          this.$message.success("操作成功")
          row.status=postData.status
          // this.getDataList()
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let postData={ids:ids.join(',')};
          api.post('rest/vabc/softwaregroup/delete',postData).then(res => {
            this.$message.success("删除成功")
            this.getDataList()
          })
        })
      },getConfig(){
        api.post('rest/vabc/softwaregroup/getConfig',{}).then(res => {
          this.groupModuleDataList=res.module
          this.groupTypeDataList=res.type
        //  this.queryForm.groupmodule=this.groupModuleDataList[0].id
          this.search()
        }).finally(() => {
          //this.dataListLoading = false
        });
      }
    }
  }
</script>
